#include "stdafx.h"

using namespace std;

int sort_file_lines( _TCHAR* filen)
{
	wstring line;
	vector<wstring> content;
	if(!filen) {
		return 0;
	}

	wifstream myfile(filen);
	if (myfile.is_open()) {
		while (getline(myfile, line)) {
			if(line.length() > 0) {
				content.push_back(line);
			}
		}
		myfile.close();
	} else
		cout << "Unable to open file";

	int* index = new int[content.size()];
	for(int i = 0; i< content.size(); i++) {
		index[i] = i;
	}

	for(int i = 0; i < content.size(); i++) {
		for(int j = i - 1; j > -1; j --) {
			int comp = StrCmpLogicalW(content.at(index[j]).c_str(), content.at(index[j+1]).c_str());
			if(comp > 0) {
				int t = index[j];
				index[j] = index[j+1];
				index[j+1] = t;
			} else
				break;
		}
	}
	
	wofstream ofile(filen);
	if (ofile.is_open())
	{
		for(int i = 0; i< content.size(); i++) {
			ofile << content.at(index[i]) << '\n';
		}
		ofile.close();
	} else
		cout << "Unable to open file";

	delete index;
	return 0;
}